<template>
  <div class="app-container" v-loading="loading">
    <el-alert
      v-if="computer.loginType == 'all'"
      style="margin-bottom: 20px"
      type="success"
      description="您的账户可以登陆所有计算机，不需要管理个人计算机"
      show-icon
      :closable="false"
    >
    </el-alert>
    <div v-disable="computer.loginType == 'all'">
      <el-card class="box-card content" style="margin-bottom: 20px">
        <div slot="header" class="clearfix">
          <span>概览</span>
        </div>
        <ul class="my-computer">
          <li>
            <label>默认计算机</label
            ><span
              ><el-link
                type="primary"
                v-if="computer.default"
                @click="showInfo(computer.default)"
                >{{ computer.default.cn }}</el-link
              ><span v-else>无</span></span
            >
          </li>
          <li>
            <label
              >私人计算机
              <el-tooltip
                class="item"
                effect="light"
                content="用户拥有的计算机，可以自行添加、删除、绑定、解绑"
                placement="top-start"
              >
                <i class="el-icon-question" /> </el-tooltip></label
            ><span v-if="computer.privateDatasource">{{
              computer.privateDatasource.length
            }}</span>
            <span v-else>0</span>
          </li>
          <li>
            <label
              >公共计算机
              <el-tooltip
                class="item"
                effect="light"
                content="管理员分配给用户的公共计算机，可以解除绑定"
                placement="top-start"
              >
                <i class="el-icon-question" /> </el-tooltip
            ></label>
            <span v-if="computer.public">{{ computer.public.length }}</span>
          </li>
        </ul>
      </el-card>
      <el-row :gutter="24">
        <el-col :xl="12" :xs="24" style="margin-bottom: 12px">
          <el-card :body-style="{ padding: '5px', minHeight: '235px' }">
            <div slot="header" class="clearfix">
              <span>私人计算机</span>
              <el-button
                style="float: right; padding: 3px 0"
                type="text"
                @click="handleAddPersonalComputer"
                >新增</el-button
              >
            </div>
            <template
              v-if="
                computer.privateDatasource &&
                computer.privateDatasource.length > 0
              "
            >
              <div class="tool-bar" style="text-align: right">
                <div
                  class="toggle"
                  style="display: inline-block; cursor: pointer"
                >
                  <el-tag
                    size="mini"
                    :type="personalDisplayType == 'card' ? 'primary' : 'info'"
                    @click="personalDisplayType = 'card'"
                  >
                    <svg-icon icon-class="list-toggle"
                  /></el-tag>
                  <el-tag
                    size="mini"
                    :type="personalDisplayType == 'list' ? 'primary' : 'info'"
                    style="margin-left: 5px"
                    @click="personalDisplayType = 'list'"
                    ><svg-icon icon-class="list"
                  /></el-tag>
                </div>
              </div>

              <div class="card-wrapper" v-if="personalDisplayType == 'card'">
                <el-card
                  v-for="(item, index) in computer.privateDatasource"
                  :key="index"
                  :class="[{ old: item.isOld }, 'computer-card']"
                  shadow="hover"
                  :body-style="{ padding: '0px' }"
                >
                  <div>
                    <div class="computer-icon">
                      <svg-icon
                        :icon-class="item.binded ? 'screen1' : 'screen2'"
                        style="width: 100%; height: 100%"
                      />
                    </div>
                    <div
                      class="tool"
                      style="padding: 10px 5px; text-align: center"
                    >
                      <el-link type="primary" @click="showInfo(item)">{{
                        item.name
                      }}</el-link>
                    </div>
                    <div style="text-align: center">
                      <el-button
                        v-show="!item.binded && !item.isOld"
                        type="text"
                        class="button"
                        size="mini"
                        @click="bind(item.samAccountName)"
                        >绑定</el-button
                      >
                      <el-button
                        v-show="item.binded"
                        type="text"
                        class="button"
                        size="mini"
                        @click="unbind(item)"
                        >解绑</el-button
                      >
                      <el-button
                        v-show="!item.binded"
                        type="text"
                        class="button"
                        size="mini"
                        style="color: red"
                        @click="delComputer(item.samAccountName)"
                        >删除</el-button
                      >
                    </div>
                  </div>
                </el-card>
              </div>
              <el-table
                :data="computer.privateDatasource"
                v-if="personalDisplayType == 'list'"
                class="dense-table"
              >
                <el-table-column
                  label="计算机账号"
                  align="left"
                  key="samAccountName"
                  prop="samAccountName"
                >
                  <template scope="scope">
                    <el-link type="primary" @click="showInfo(scope.row)">{{
                      scope.row.cn
                    }}</el-link>
                  </template>
                </el-table-column>

                <el-table-column
                  label="计算机名称"
                  align="left"
                  key="name"
                  prop="name"
                />
                <el-table-column width="100px" label="操作" align="left">
                  <template slot-scope="{ row }">
                    <el-button
                      v-if="!row.binded && !row.isOld"
                      size="mini"
                      type="text"
                      @click="bind(row.samAccountName)"
                      >绑定</el-button
                    >
                    <el-button
                      v-if="row.binded"
                      size="mini"
                      type="text"
                      @click="unbind(row)"
                      >解绑</el-button
                    >
                  </template>
                </el-table-column>
              </el-table>
            </template>
            <template v-else>
              <div
                class="empty-container"
                style="height: 150px; text-align: center; padding: 40px"
              >
                <svg-icon
                  style="width: 100%; margin-bottom: 10px; height: 80px"
                  icon-class="no-data"
                ></svg-icon>
                <span style="padding: 10px">无数据</span>
              </div>
            </template>
          </el-card>
        </el-col>
        <el-col :xl="12" :xs="24">
          <el-card :body-style="{ padding: '5px', minHeight: '235px' }">
            <div slot="header" class="clearfix">
              <span>公共计算机</span>
            </div>
            <template v-if="computer.public && computer.public.length > 0">
              <div class="tool-bar" style="text-align: right">
                <div
                  class="toggle"
                  style="display: inline-block; cursor: pointer"
                >
                  <el-tag
                    size="mini"
                    :type="publicDisplayType == 'card' ? 'primary' : 'info'"
                    @click="publicDisplayType = 'card'"
                  >
                    <svg-icon icon-class="list-toggle"
                  /></el-tag>
                  <el-tag
                    size="mini"
                    :type="publicDisplayType == 'list' ? 'primary' : 'info'"
                    style="margin-left: 5px"
                    @click="publicDisplayType = 'list'"
                    ><svg-icon icon-class="list"
                  /></el-tag>
                </div>
              </div>
              <div class="card-wrapper" v-if="publicDisplayType == 'card'">
                <el-card
                  class="computer-card"
                  v-for="(item, index) in computer.public"
                  :key="index"
                  shadow="hover"
                  :body-style="{ padding: '0px' }"
                >
                  <div>
                    <div class="computer-icon">
                      <svg-icon
                        :icon-class="item.binded ? 'screen1' : 'screen2'"
                        style="width: 100%; height: 100%"
                      />
                    </div>
                    <div
                      class="tool"
                      style="padding: 10px 5px; text-align: center"
                    >
                      <el-link type="primary" @click="showInfo(item)">{{
                        item.name
                      }}</el-link>
                    </div>
                    <div style="text-align: center">
                      <el-button
                        v-show="item.binded"
                        type="text"
                        class="button"
                        size="mini"
                        @click="unbind(item, 'public')"
                        >解绑</el-button
                      >
                      <el-button
                        v-show="!item.binded"
                        type="text"
                        class="button"
                        size="mini"
                        @click="bind(item, 'public')"
                        >绑定</el-button
                      >
                    </div>
                  </div>
                </el-card>
              </div>
              <el-table
                class="dense-table"
                v-if="publicDisplayType == 'list'"
                :data="computer.public"
              >
                <el-table-column
                  label="计算机账号"
                  align="left"
                  key="name"
                  prop="name"
                >
                  <template scope="scope">
                    <el-link type="primary" @click="showInfo(scope.row)">{{
                      scope.row.cn
                    }}</el-link>
                  </template>
                </el-table-column>

                <el-table-column
                  label="计算机名称"
                  align="left"
                  key="name"
                  prop="name"
                />
                <el-table-column width="100px" label="操作" align="left">
                  <template slot-scope="{ row }">
                    <el-button
                      v-show="item.binded"
                      size="mini"
                      type="text"
                      @click="unbind(row, 'public')"
                      >解绑</el-button
                    >
                    <el-button
                      v-show="!item.binded"
                      size="mini"
                      type="text"
                      @click="bind(row, 'public')"
                      >绑定</el-button
                    >
                  </template>
                </el-table-column>
              </el-table>
            </template>
            <template v-else>
              <div
                class="empty-container"
                style="height: 150px; text-align: center; padding: 40px"
              >
                <svg-icon
                  style="width: 100%; margin-bottom: 10px; height: 80px"
                  icon-class="no-data"
                ></svg-icon>
                <span style="padding: 10px">无数据</span>
              </div>
            </template>
          </el-card>
        </el-col>
      </el-row>
    </div>

    <!-- 用户详情抽屉 -->
    <el-drawer
      :withHeader="false"
      size="600px"
      v-if="currentComputer"
      :visible.sync="computerDetailDrawerVisible"
      direction="rtl"
      :append-to-body="true"
      :modal-append-to-body="true"
      :modal="false"
    >
      <div
        class="info-container"
        v-loading="infoLoading"
        style="padding: 50px 15px; height: 100%"
      >
        <Description-form :data="currentComputer">
          <Description-item label="计算机名称" prop="name" />
          <Description-item label="计算机位置" prop="distinguishedName" />
          <Description-item label="创建时间" prop="whenCreated" />
          <Description-item label="操作系统" prop="operatingSystem" />
          <Description-item label="设备版本" prop="operatingSystemVersion">
          </Description-item>
          <Description-item label="已绑定用户">
            <template>
              <div
                class="bindUser"
                v-for="(person, index) in currentComputer.bindUsers"
                :key="index"
              >
                <span style="margin-right: 20px">{{
                  person.samAccountName
                }}</span>
                <span>{{ person.fullDepartment }}</span>
              </div>
            </template>
          </Description-item>
        </Description-form>
      </div>
    </el-drawer>
  </div>
</template>

<script>
import personalComputerApi from "@/api/activeDirectory/personalComputer";

export default {
  name: "personComputer",
  data() {
    return {
      // 遮罩层
      loading: false,

      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,

      computer: {
        default: undefined,
        personal: [],
        public: [],
        privateDatasource: [],
      },
      personalDisplayType: "card",
      publicDisplayType: "card",

      currentComputer: {
        distinguishedName: "",
        info: "",
        name: "",
        objectCategory: "",
        objectGUID: "",
        samAccountName: "",
        whenCreated: "",
      },

      infoLoading: false,
      computerDetailDrawerVisible: false,

      addPersonalComputerDialogVisible: false,
    };
  },

  mounted() {
    this.getData();
  },

  activated() {
    this.getData();
  },
  methods: {
    /** 查询用户列表 */
    getData() {
      this.loading = true;
      personalComputerApi
        .list()
        .then((res) => {
          // this.computer = res.data;
          // var { privateDatasource, personal } = res.data;

          // privateDatasource = privateDatasource.map(n => {
          //   var binded = personal.find(r => r.cn == n.cn) != null;
          //   var temp = { ...n, binded };
          //   return temp;
          // });
          // console.log(privateDatasource);
          this.computer = res.data;
          // this.computer.privateDatasource = privateDatasource;
        })
        .catch()
        .finally((n) => {
          this.loading = false;
        });
    },

    async bind(samAccountName) {
      this.loading = true;
      const res = await personalComputerApi
        .bind(samAccountName)
        .catch()
        .finally((n) => {
          this.loading = false;
        });
      if (res.success) {
        this.$message.success("绑定成功！");
        this.getData();
      }
    },

    async unbind({ samAccountName, name, binded, isOld }, type = "personal") {
      if (type == "personal" && binded && isOld) {
        this.$confirm(
          `计算机【${name}】为历史数据，解绑后无法重新绑定，请谨慎操作？`,
          "提示",
          {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
          }
        ).then(() => {
          this.doUnbindRequest(samAccountName, type);
        });
      } else {
        this.doUnbindRequest(samAccountName, type);
      }
    },
    async doUnbindRequest(samAccountName, type) {
      this.loading = true;
      const res = await personalComputerApi
        .unbind(samAccountName, type)
        .catch()
        .finally((n) => {
          this.loading = false;
        });
      if (res.success) {
        this.$message.success("解绑成功！");
        this.getData();
      }
    },
    async handleAddPersonalComputer() {
      this.$confirm("私人计算机会根据您的账户自动命名，确定新增吗", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(async () => {
        this.loading = true;
        const res = await personalComputerApi
          .addPersonComputer()
          .catch()
          .finally((n) => {
            this.loading = false;
          });
        if (res.success) {
          this.$message.success("新增成功");
          this.getData();
        }
      });
    },

    async delComputer(name) {
      this.$confirm(`确定删除计算机【${name}】吗？`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(async () => {
        this.loading = true;
        const res = await personalComputerApi
          .deleteComputer(name)
          .catch()
          .finally((n) => {
            this.loading = false;
          });
        if (res.success) {
          this.$message.success("删除成功");
          this.getData();
        }
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        userId: undefined,
        deptId: undefined,
        userName: undefined,
        nickName: undefined,
        password: undefined,
        phonenumber: undefined,
        email: undefined,
        sex: undefined,
        status: "0",
        remark: undefined,
        postIds: [],
        roleIds: [],
      };
      this.resetForm("form");
    },

    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.userId);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },

    //查看详细信息
    async showInfo(row) {
      this.computerDetailDrawerVisible = true;
      this.infoLoading = true;
      const res = await personalComputerApi
        .info(row.samAccountName)
        .catch()
        .finally((n) => {
          this.infoLoading = false;
        });
      if (res.success) {
        this.currentComputer = res.data;
      }
    },
  },
};
</script>
<style lang="scss" scoped>
@import "./style/computerCard.scss";
.head-container {
  max-height: calc(100vh - 100px);
  overflow: auto;
}
.tool-bar {
  padding: 5px;
}
.my-computer {
  display: flex;
  justify-content: space-between;
  margin: 0;
  padding: 0;
  > li {
    list-style: none;
    width: 33%;
    border-right: 1px solid #e6e9e9;
    margin: 10px;
    > label {
      display: block;
      color: #555 !important;
      margin-bottom: 10px;
      font-size: 14px;
    }
    > span {
      font-size: 24px;
      color: #ff7a4b;
      margin-right: 10px;
      vertical-align: baseline;
    }
  }
}
.disabled {
  position: relative;
  cursor: not-allowed;
  &::after {
    content: "";
    position: absolute;
    left: 0;
    top: 0;
    z-index: 99;
    width: 100%;
    height: 100%;
    // backdrop-filter: blur(5px);
    background: rgba(0, 0, 0, 0.2);
    // opacity: 0.3;
  }
}
.computer-info {
  .info-container {
    font-family: "Helvetica Neue", Helvetica, "PingFang SC", "Hiragino Sans GB",
      "Microsoft YaHei", "微软雅黑", Arial, sans-serif;
    color: #606266;

    .info-group-header {
      border-left-color: #409eff;
      border-left-width: 5px;
      border-radius: 3px;
      border-left-style: solid;
      padding-left: 6px;
    }
    .info-item {
      margin: 15px;
      .info-label {
        display: inline-block;
        min-width: 100px;
        text-align: right;
        margin-right: 5px;
        font-weight: bold;
        color: #303133;
      }
      &.inline {
        display: inline-block;
        width: 40%;
        margin: 0px;
        margin-right: 15px;
      }
    }
  }
}
.app-container {
  .el-table {
    height: auto;
    .el-table__body-wrapper {
      overflow: auto;
      height: 100%;
      padding: 0 13px 30px 0;
    }
  }
}
</style>

<style lang="scss">
.app-main {
  min-width: auto !important;
}
.bindUser {
  display: flex;
  justify-content: space-between;
  padding-bottom: 10px;
  &:not(:last-child) {
    border-bottom: 1px solid rgb(228, 231, 237);
  }
  &:not(:first-child) {
    padding-top: 10px;
  }
}
</style>
